package junit.base;

import com.jfinal.kit.Base64Kit;
import io.jboot.Jboot;
import io.jboot.test.MockMvcResult;
import io.jboot.test.junit4.JbootRunner;
import io.jboot.utils.StrUtil;
import io.jpress.JPressConfig;
import io.jpress.commons.utils.RsaUtil;
import io.jpress.web.interceptor.CSRFInterceptor;
import org.junit.runner.RunWith;

import javax.servlet.http.Cookie;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@RunWith(JbootRunner.class)
public class AdminBaseTester extends BaseTester {


    public static void doLogin() {
        String loginUser = Jboot.configValue("test.login.user");
        String loginPwd = Jboot.configValue("test.login.password");

        if (StrUtil.isAnyBlank(loginUser,loginPwd)){
            throw new IllegalStateException("Please config test user info.");
        }

        doLogin(loginUser, loginPwd);
    }


    public static MockMvcResult doLogin(String user, String pwd) {
        //关闭验证码验证
        JPressConfig.me.setAdminLoginCaptchaValidateEnable(false);

        //判断是否开启 rsa 登录加密功能
        if (StrUtil.isNotBlank(JPressConfig.me.getFrontPublicKey())) {
            try {
                final PublicKey publicKey = RsaUtil.getPublicKey(JPressConfig.me.getFrontPublicKey());
                final byte[] bytes = RsaUtil.encryptByPublicKey(pwd.getBytes(), publicKey);
                pwd = Base64Kit.encode(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return mvc.post("/admin/doLogin", buildParas("user:" + user, "pwd:" + pwd));
    }


    public static MockMvcResult getWithCsrfToken(String action) {
        return getWithCsrfToken(action, null);
    }

    public static MockMvcResult getWithCsrfToken(String action, Map<String, Object> paras) {
        if (paras == null) {
            paras = new HashMap<>();
        }

        paras.put(CSRFInterceptor.CSRF_KEY, "123");
        Set<Cookie> cookies = new HashSet<>();
        cookies.add(new Cookie(CSRFInterceptor.CSRF_KEY, "123"));

        return mvc.get(action, paras, null, cookies);
    }

    public static MockMvcResult postWithCsrfToken(String action, Map<String, Object> paras) {
        if (paras == null) {
            paras = new HashMap<>();
        }

        paras.put(CSRFInterceptor.CSRF_KEY, "123");
        Set<Cookie> cookies = new HashSet<>();
        cookies.add(new Cookie(CSRFInterceptor.CSRF_KEY, "123"));

        return mvc.post(action, paras, null, cookies, null);
    }

    public static MockMvcResult uploadWithCsrfToken(String action, Map<String, Object> paras) {
        if (paras == null) {
            paras = new HashMap<>();
        }

        paras.put(CSRFInterceptor.CSRF_KEY, "123");
        Set<Cookie> cookies = new HashSet<>();
        cookies.add(new Cookie(CSRFInterceptor.CSRF_KEY, "123"));

        return mvc.upload(action, paras, null, cookies);
    }


    public static MockMvcResult doLogout() {
        return mvc.get("/admin/logout");
    }


}
